package dao;

import entry.Books;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class BookDaoImpl implements BookDao {
    DBHelper dbHelper = new DBHelper();
    @Override
    public int insertBook(Books book) {
        String sql = "insert into books (bookname, bookauthor, bookpublish, bookpage, bookprice, createdate) values(?, ?, ?, ?, ?, ?)";
        return dbHelper.changeInfo(sql, book.getBookname(), book.getBookauthor(), book.getBookpublish(), book.getBookpage(), book.getBookprice(), book.getCreatedate());
    }

    @Override
    public boolean delete(int bookid) {
        String sql = "delete from books where bookid = ?";
        return dbHelper.changeInfo(sql, bookid) != 0 ? true : false;
    }

    @Override
    public Books selectById(int bookid) {
        String sql = "select * from books where bookid = ?";
        return (Books)(dbHelper.selectInfo(sql, Books.class, bookid).get(0));
    }

    @Override
    public boolean updateBook(Books book) {
        String sql = "update books set bookname = ?, bookauthor = ?, bookpublish = ?, bookpage = ?, bookprice = ?, createdate = ? where bookid = ?";
        return  dbHelper.changeInfo(sql, book.getBookname(), book.getBookauthor(), book.getBookpublish(), book.getBookpage(), book.getBookprice(), book.getCreatedate(), book.getBookid()) != 0 ? true : false;
    }

    @Override
    public ArrayList<Books> selectMatch(Books book, PageHelper pageHelper) {
        StringBuffer sql = new StringBuffer("select * from books where 1 = 1 ");
        ArrayList list = new ArrayList();
        if (book.getBookname() != null) {
            sql.append(" and bookname like ?");
            list.add("%" + book.getBookname() + "%");
        }
        if (book.getBookauthor() != null) {
            sql.append(" and bookauthor like ?");
            list.add("%" + book.getBookauthor() + "%");
        }
        if (book.getBookpublish() != null) {
            sql.append(" and bookpublish like ?");
            list.add("%" + book.getBookpublish() + "%");
        }
        sql.append(" order by createdate desc limit ?, ? ");
        int startIndex = (pageHelper.getWhichPage() - 1) * pageHelper.getPerPage();
        list.add(startIndex);
        list.add(pageHelper.getPerPage());
        System.out.println(startIndex+"....daoimpl");
        return dbHelper.selectInfo(sql.toString(), Books.class, list.toArray());
    }

    @Override
    public int selectBookSize(Books book) {
        StringBuffer sql = new StringBuffer("select count(bookid) as bookid from books where 1 = 1 ");
        ArrayList list = new ArrayList();
        if (book.getBookname() != null) {
            sql.append(" and bookname like ?");
            list.add("%" + book.getBookname() + "%");
        }
        if (book.getBookauthor() != null) {
            sql.append(" and bookauthor like ?");
            list.add("%" + book.getBookauthor() + "%");
        }
        if (book.getBookpublish() != null) {
            sql.append(" and bookpublish like ?");
            list.add("%" + book.getBookpublish() + "%");
        }
        System.out.println(list.toArray());
        int count = dbHelper.selectInfoCount(sql.toString(), list.toArray());
        System.out.println("dao="+count);
        return count;
    }
}
